<?php

namespace fakis\config;

/**
 * 配置项规范化定义
 *
 * 编码规范
 *
 * - 每个常量的定义作为一个配置项，配置项名称只能为英文字母、数字和下划线
 * ```php
 * const CONSTANT_NAME = 'constant_name';
 * ```
 *
 * - `props()`静态方法，可定义每个配置项的规则，规则可参考`\fakis\core\base\DynamicProp`
 * ```php
 * public static function props(): array
 * {
 *     return [
 *         self::S1 => DynamicProp::TYPE_STRING,
 *         self::CONSTANT_NAME => [
 *             'label' => '常量名称',
 *             'format' => 'boolean',
 *             'hints' => '',
 *             'default' => true,
 *             'required' => false,
 *         ]
 *     ];
 * }
 * ```
 *
 * @author Fakis <fakis738@qq.com>
 */
class Constant implements ConstantInterface
{
    /**
     * 定义属性
     * @return array|\fakis\core\base\DynamicProp[]
     */
    public static function props(): array
    {
        return [];
    }

    /**
     * 提供给模型层解析属性的接口
     * @return array
     */
    public static function attributes(): array
    {
        $attributes = [];
        $constants = (new \ReflectionClass(static::class))->getConstants();

        foreach ($constants as $constant) {
            $attributes[$constant] = null;
        }

        foreach (static::props() as $name => $value) {
            if (in_array($name, $constants, true)) {
                $attributes[$name] = $value;
            }
        }

        return $attributes;
    }
}